package com.villaday.idf.activity;

import android.content.Intent;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter2.SuCaiTagsAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.CustomPopupWindow;
import com.villaday.idf.custom.DividerItemDecoration.RVItemDecoration;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.fragment.HomeListDynamicFragment;
import com.villaday.idf.model2.Material_tag_list;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToolBarUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * 动态素材
 *
 * */
public class DynamicActivity extends BaseActivity implements OkHttpUtil.OnDataListener, AbsRecyclerViewAdapter.OnItemClickListener {


    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    private LoadingDialog loadingDialog;

    private Gson gson = new GsonBuilder().create();
    private SuCaiTagsAdapter adapter;
    private int currentPosition = -1;//当前选中的position
    private Material_tag_list material_tag_list;
    private HomeListDynamicFragment homeListDynamicFragment;
    private TagAdapter<Material_tag_list.BodyEntity> tagAdapter;

    @Override
    protected int getViewResId() {
        return R.layout.activity_dynamic;
    }


    @OnClick(R.id.img)
    public void onViewClicked() {

        CustomPopupWindow.Builder builder = new CustomPopupWindow.Builder();
        final CustomPopupWindow builder1 = builder
                .setheight(LinearLayoutCompat.LayoutParams.MATCH_PARENT)
                .setwidth(LinearLayoutCompat.LayoutParams.MATCH_PARENT)
                .setOutSideCancel(true)
                .setContext(this)
                .setContentView(R.layout.popu_dynamic2)
                .builder();
        final TagFlowLayout idFlowlayout = (TagFlowLayout) builder1.getItemView(R.id.id_flowlayout);


        builder1.showAsLaction(toolbarText, Gravity.BOTTOM, 0, 0);

        builder1.getItemView(R.id.img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder1.dismiss();
            }
        });

        builder1.getItemView(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder1.dismiss();
            }
        });


        if (material_tag_list != null && material_tag_list.getBody() != null) {
            tagAdapter = new TagAdapter<Material_tag_list.BodyEntity>(material_tag_list.getBody()) {
                @Override
                public View getView(FlowLayout parent, int position, Material_tag_list.BodyEntity s) {
                    View view = getLayoutInflater().inflate(R.layout.item_bourn_head_chosecity_tab,
                            idFlowlayout, false);
                    TextView tv = view.findViewById(R.id.tv_city);

                    tv.setText(s.getName());
                    LinearLayout.LayoutParams hint_page_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    hint_page_params.setMargins(0, 0, PhoneUtil.dp2px(DynamicActivity.this, 5), PhoneUtil.dp2px(DynamicActivity.this, 5));//设置左右边距
                    tv.setLayoutParams(hint_page_params);

                    return view;
                }
            };
            idFlowlayout.setAdapter(tagAdapter);
            idFlowlayout.specifyLines(9999);
            if (currentPosition != -1) {
                tagAdapter.setSelectedList(currentPosition);
            }

            idFlowlayout.getSelectedList();
            idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()

            {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    builder1.dismiss();

                    if (currentPosition != -1 && currentPosition != position) {
                        material_tag_list.getBody().get(currentPosition).setCheck(false);

                    }
                    material_tag_list.getBody().get(position).setCheck(!material_tag_list.getBody().get(position).isCheck());

                    adapter.notifyDataSetChanged();
                    if (material_tag_list.getBody().get(position).isCheck()) {
                        currentPosition = position;
                    } else {
                        currentPosition = -1;

                    }
                    rv.scrollToPosition(position);
                    homeListDynamicFragment.setTagId(material_tag_list.getBody().get(position).isCheck() ? material_tag_list.getBody().get(position).getId() : "");
                    homeListDynamicFragment.onRefresh();

                    return true;
                }
            });
        }
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "动态素材", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        }, "我的收藏", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(DynamicActivity.this, MyCollectActivity.class));
            }
        });
        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getFragmentManager());
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        homeListDynamicFragment = HomeListDynamicFragment.newInstance();
        transaction.add(R.id.fl, homeListDynamicFragment, "0");
        transaction.commitAllowingStateLoss();

        adapter = new SuCaiTagsAdapter(this);
        rv.addItemDecoration(new RVItemDecoration(LinearLayoutCompat.HORIZONTAL, PhoneUtil.dp2px(this, 5), getResources().getColor(R.color.colorWhite), PhoneUtil.dp2px(this, 1)));
        adapter.setFullScreen(true);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rv.setAdapter(adapter);
        adapter.setOnItemClickListener(this);


    }

    @Override
    protected void loadData() {

        OkHttpUtil.getJSON(this, Constant.URL.material_tag_list, this);

    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }


    }

    @Override
    public void onResponse(String url, String json) {
        dismissLoading();
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.material_tag_list.equals(url)) {
                LogeUtil.e("material_tag_list", json);
                material_tag_list = gson.fromJson(json, Material_tag_list.class);
                if ("200".equals(material_tag_list.getCode())) {

                    if (material_tag_list.getBody().size() > 0) {
                        material_tag_list.getBody().get(0).setCheck(true);
                        currentPosition=0;
                    }

                    adapter.setData(material_tag_list.getBody());

                }
            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }


    @Override
    public void onItemClick(View v, int position) {
        if (currentPosition != -1 && currentPosition != position) {
            material_tag_list.getBody().get(currentPosition).setCheck(false);

        }
        material_tag_list.getBody().get(position).setCheck(!material_tag_list.getBody().get(position).isCheck());
        adapter.notifyDataSetChanged();
        if (material_tag_list.getBody().get(position).isCheck()) {
            currentPosition = position;
        } else {
            currentPosition = -1;

        }


        homeListDynamicFragment.setTagId(material_tag_list.getBody().get(position).isCheck() ? material_tag_list.getBody().get(position).getId() : "");
        homeListDynamicFragment.onRefresh();

    }


    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {

            super.onBackPressed();
        }
    }
}

